import { exec } from "child_process";
import { Task } from "../task";
import { LaunchArguments, ModernVersion } from "../version";
import { Java } from "../download";
import { IProfile } from "../profile";

class TaskLaunchGame extends Task {
    id = "launch_game";
    version: ModernVersion;
    java: Java;
    profile: IProfile;
    arguments: LaunchArguments;

    get dependOn(): string[] {
        return [
            "download_libraries",
            "extract_natives",
            "download_assetindex",
            "download_assets",
            "download_client_jar",
        ];
    }

    constructor(
        version: ModernVersion,
        java: Java,
        profile: IProfile,
        arguments_: LaunchArguments,
    ) {
        super();
        this.version = version;
        this.java = java;
        this.profile = profile;
        this.arguments = arguments_;
    }

    run(): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            const child = exec(this.version.launch(this.java, this.profile, this.arguments));

            child.on("exit", (code)=>{
                resolve(code===0);
            });
        });
    }

    get progress(): number | null {
        return null;
    }
}

export { TaskLaunchGame };
